package com.simple.datax.filebackup.impl;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.simple.datax.SimpleMessage;
import com.simple.datax.SimpleObject;
import com.simple.datax.api.SimpleException;
import com.simple.datax.service.FileBackupService;
import com.simple.datax.service.SimpleFileException;
import com.simple.datax.service.impl.TestMsgProcessorImpl;
import com.simple.datax.utils.DateUtils;

/**
 * @Title: FileBackupServiceImpl.java
 * @Package com.simple.datax.filebackup.impl
 * @Description: 文件备份服务实现
 * @author Paladin Chen
 * @date 2012-12-25 上午9:54:11
 * @version V1.0
 */
public class FileBackupServiceImpl implements FileBackupService {

	private static final Logger logger = LoggerFactory.getLogger(TestMsgProcessorImpl.class);

	private String configPath = "../backup";

	/**
	 * 备份类型：1、原始备份；2、解析后备份；3、发送备份
	 */
	private int backupType = 1;

	@Override
	public void process(SimpleObject src) throws SimpleException {
		if (src instanceof SimpleMessage) {
			try {
				logger.debug("Begin to write file!" + ((SimpleMessage) src).toLog());

				String fileName = getBackupFileName((SimpleMessage) src);
				File writefile = new File(fileName);

				// System.out.println(fileName);

				// try{
				//
				// } catch (Exception e){
				//
				// }
				// TODO: 要做内容的编码转换
				FileUtils.writeByteArrayToFile(writefile, ((SimpleMessage) src).getContent().getBytes());
			} catch (Exception e) {
				//logger.error(((SimpleMessage) src).toLog() + "Write File Error:", e);
				throw new SimpleFileException("Write File Error", e);
			}

			logger.debug("End write file!" + ((SimpleMessage) src).toLog());
		} else {
			//logger.error("This function Only proc SimpleMessage!!");
			throw new SimpleFileException("The Parameter is NOT SimpleMessage!!");
		}
	}

	public String getConfigPath() {
		return configPath;
	}

	public void setConfigPath(String configPath) {
		this.configPath = configPath;
	}

	public int getBackupType() {
		return backupType;
	}

	public void setBackupType(int backupType) {
		this.backupType = backupType;
	}

	/**
	 * 要求外面配置的路径不带末尾的分隔符
	 * 
	 * @return
	 */
	private String getBackupFileName(SimpleMessage src) {
		StringBuffer result = new StringBuffer(configPath);
		String fileSep = System.getProperty("file.separator");

		String communType = src.getCommunType();
		
		switch (backupType) {
		case 2:
			result.append(fileSep).append("ParseMessage");
			break;
		case 3:
			result.append(fileSep).append("SentMessage");
			communType = src.getSentCommunType();
			break;
		default:
			result.append(fileSep).append("OriMessage");
			break;
		}

		result.append(fileSep).append(DateUtils.getTimeStamp("yyyyMMdd"));

		
		
		if (StringUtils.isBlank(communType)) {
			result.append(fileSep).append("default");
		} else {
			result.append(fileSep).append(communType);
		}

		result.append(fileSep);

		switch (backupType) {
		case 2:
			result.append(fileSep).append(src.getFileName(false)).append(".").append(src.getMsgFormat());
			break;
		case 3:
			result.append(fileSep).append(src.getFileName()).append(".").append(src.getSentMsgFormat());
			break;
		default:
			result.append(fileSep).append(src.getOriFileName(false)).append(".").append(src.getMsgFormat());
			break;
		}

		return result.toString();
	}

}
